草庐IT

C++ union 与 reinterpret_cast

全部标签

c++ - 实践中的 union 、别名和类型双关语 : what works and what does not?

我无法理解使用带有GCC的union可以做什么和不可以做什么。我阅读了有关它的问题(特别是here和here),但它们关注的是C++标准,我觉得C++标准和实践(常用的编译器)之间存在不匹配。特别是,我最近在GCConlinedoc中发现了令人困惑的信息。在阅读编译标志-fstrict-aliasing时。它说:-fstrict-aliasingAllowthecompilertoassumethestrictestaliasingrulesapplicabletothelanguagebeingcompiled.ForC(andC++),thisactivatesoptimizati

c++ - 实践中的 union 、别名和类型双关语 : what works and what does not?

我无法理解使用带有GCC的union可以做什么和不可以做什么。我阅读了有关它的问题(特别是here和here),但它们关注的是C++标准,我觉得C++标准和实践(常用的编译器)之间存在不匹配。特别是,我最近在GCConlinedoc中发现了令人困惑的信息。在阅读编译标志-fstrict-aliasing时。它说:-fstrict-aliasingAllowthecompilertoassumethestrictestaliasingrulesapplicabletothelanguagebeingcompiled.ForC(andC++),thisactivatesoptimizati

c++ - 使用 reinterpret_cast 将函数强制转换为 void*,为什么不违法?

这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher

c++ - 使用 reinterpret_cast 将函数强制转换为 void*,为什么不违法?

这是对我之前的问题Theaddressofafunctionmatchingaboolvsconstvoid*overload的切线跟进.回答者解释说:The[C++11]standarddoesnotdefineanystandardconversionsfroma"pointertofunction"toa"pointertovoid."It'shardtoprovideaquotefortheabsenceofsomething,buttheclosestIcandoisC++114.10/2[conv.ptr]:Aprvalueoftype“pointertocvT,”wher

c++ - reinterpret_cast moSTLy 没用吗?

我已经阅读了关于theuseofreinterpret_cast的各种先前问题。,并且我还阅读了C++标准中的相关措辞。本质上,它归结为指针到指针reinterpret_cast操作的结果不能安全地用于任何东西,除了被转换回原始指针类型。然而,在实践中,reinterpret_cast的大多数实际使用似乎是基于(错误的)假设,即reinterpret_cast与C风格相同throw。例如,我看到很多代码使用reinterpret_cast将char*转换为unsignedchar*以进行字符集转换例行公事。这是完全无害的,但严格来说它不是可移植的-不能保证从char*到unsigned

c++ - reinterpret_cast moSTLy 没用吗?

我已经阅读了关于theuseofreinterpret_cast的各种先前问题。,并且我还阅读了C++标准中的相关措辞。本质上,它归结为指针到指针reinterpret_cast操作的结果不能安全地用于任何东西,除了被转换回原始指针类型。然而,在实践中,reinterpret_cast的大多数实际使用似乎是基于(错误的)假设,即reinterpret_cast与C风格相同throw。例如,我看到很多代码使用reinterpret_cast将char*转换为unsignedchar*以进行字符集转换例行公事。这是完全无害的,但严格来说它不是可移植的-不能保证从char*到unsigned

c++ - 匿名 union 和结构

这个问题在这里已经有了答案:WhydoesC++disallowanonymousstructs?(7个回答)关闭8年前。您将如何在标准C++11/14中执行此操作?因为如果我没记错的话,这不是具有匿名结构的标准兼容代码。我希望以与您相同的方式访问成员。templatestructvec{union{struct{some_typex,y,z;};struct{some_typer,g,b;};some_typeelements[3];};}; 最佳答案 是的,C++11和C++14都不允许匿名结构。Thisanswer包含为什么会

c++ - 匿名 union 和结构

这个问题在这里已经有了答案:WhydoesC++disallowanonymousstructs?(7个回答)关闭8年前。您将如何在标准C++11/14中执行此操作?因为如果我没记错的话,这不是具有匿名结构的标准兼容代码。我希望以与您相同的方式访问成员。templatestructvec{union{struct{some_typex,y,z;};struct{some_typer,g,b;};some_typeelements[3];};}; 最佳答案 是的,C++11和C++14都不允许匿名结构。Thisanswer包含为什么会

c++ - dynamic_cast 如何失败?

根据我的阅读,执行错误的运行时dynamic_cast可能会抛出bad_cast异常或返回零。如果你正在转换指针,它会返回零是否正确?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Base*base=newBase();dynamic_cast(base);return0;}而且它会在转换对象时抛出一个bad_cast异常?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Basebase;Base&ref=base;d

c++ - dynamic_cast 如何失败?

根据我的阅读,执行错误的运行时dynamic_cast可能会抛出bad_cast异常或返回零。如果你正在转换指针,它会返回零是否正确?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Base*base=newBase();dynamic_cast(base);return0;}而且它会在转换对象时抛出一个bad_cast异常?即:classBase{virtualvoida(){}};classDerived:publicBase{};intmain(){Basebase;Base&ref=base;d